/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.cacheplugins  
 */
package com.apache.bid.service.plugins;

import java.util.Date;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.BidCommonUtil;
import com.apache.bid.common.Constant;
import com.apache.bid.common.WbdToolsUtil;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.common.vo.BidItemCacheVo;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidPub;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.BidPubManager;
import com.apache.bid.service.cacheplugins.helper.ReductionPriceHelper;
import com.apache.cache.util.Validator;
import com.apache.tools.DateUtils;

/**
 * description:  保存并发布标的信息
 * @author Hou Dayu 创建时间：2016-3-23  
 * 
 */
public class SaveItemPluginImpl implements PluginConnector {

	protected Logger log = Logger.getLogger(getClass());
	protected BidItemManager bidItemManager;
	protected BidPubManager bidPubManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		log.info("保存标的，控制是否发布");
		BidItem item = (BidItem) param.getObj();
		ResultEntity entity = new ResultEntity();
		if (Validator.isEmpty(item)) {
			entity.setResult("false");
			entity.setMessage("缺少方法参数,方法执行失败");
		} else {
			ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
			setCloseTime(item);
			vo.setObj(item);
			String itemId = bidItemManager.saveInfo(vo);
			BidItemCacheVo cacheVo = new BidItemCacheVo();
			if ("0".equals(item.getSessionId())) {
				BidPub pub = new BidPub();
				pub = (BidPub) WbdToolsUtil.getInstance().convert2Bean(param.getParams(), pub);
				ParamsVo pubVo = new ParamsVo();
				pub.setItemId(itemId);
				pubVo.setObj(pub);
				cacheVo.setBjServer(pub.getBjServer());
				cacheVo.setBjPort(pub.getBjPort());
				cacheVo.setPub(pub);
				bidPubManager.saveInfo(pubVo);
			}
			///判断是否需要发布标的(此处有两种情况，1：活动下的标的，2、单标的情况)
			if (Constant.getInstance().getItemConfig("statusPubslish").equals(item.getItemStatus())) {
				if (!"0".equals(item.getSessionId())) {
					BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("session");
					BidSession session = (BidSession) cache.getCacheObjectByKey(cache.session_prefix
							+ item.getSessionId());
					BidPub bidPub = (BidPub) cache.getCacheObjectByKey(cache.pub_prefix + item.getSessionId());
					cacheVo.addItemInfo(item);
					cacheVo.setBailCtrl(session.getBailCtrl());
					cacheVo.setBjServer(bidPub.getBjServer());
					cacheVo.setBjPort(bidPub.getBjPort());
					UnityItemPluginHelper.getInstance().startItem(cacheVo, true);
				} else {
					cacheVo.addItemInfo(item);
					UnityItemPluginHelper.getInstance().startItem(cacheVo, true);
				}
				if ("T".equals(item.getvFlag()) || Constant.BIDMODE_HELAN.equals(item.getBidMode())) {
					ReductionPriceHelper.getInstance().setParams(itemId, itemId);
				}
			}

			entity.setResult("true");
			entity.setMessage("规则信息保存成功");
			entity.setEntity(itemId);
		}
		return entity;
	}

	private void setCloseTime(BidItem item) throws Exception {
		if (Constant.BIDMODE_JTZX.equals(item.getBidMode()) || Constant.BIDMODE_JTFX.equals(item.getBidMode())) {
			String colseTime = BidCommonUtil.getInstance().getMergeTime(item.getFirstBidTimePre(), item.getLimitTime());
			item.setCloseTimePre(colseTime);
		} else if (Constant.BIDMODE_YCXASC.equals(item.getBidMode())
				|| Constant.BIDMODE_YCXDESC.equals(item.getBidMode())) {
			item.setCloseTimePre(item.getFirstBidTimePre());
		} else if (Constant.BIDMODE_HELAN.equals(item.getBidMode())) {
			long limittime = Long.valueOf(item.getLimitTime()) * 1000 * 2;
			Date data = new Date(DateUtils.toLong(DateUtils.parse(item.getFirstBidTimePre(), "yyyy-MM-dd HH:mm:ss"))
					+ limittime);
			String colseTime = DateUtils.format(data, "yyyy-MM-dd HH:mm:ss");
			item.setCloseTimePre(colseTime);
		} else if (Constant.BIDMODE_FEJJZX.equals(item.getBidMode())
				|| Constant.BIDMODE_FEJJFX.equals(item.getBidMode())) {
			String colseTime = BidCommonUtil.getInstance().getMergeTime(item.getFirstBidTimePre(), item.getLimitTime());
			item.setCloseTimePre(colseTime);
		}
	}

	public void setBidItemManager(BidItemManager bidItemManager) {
		this.bidItemManager = bidItemManager;
	}

	public void setBidPubManager(BidPubManager bidPubManager) {
		this.bidPubManager = bidPubManager;
	}

}
